package com.itboye.app.mian.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ImageView.ScaleType;

import com.itboye.config.GlobalConfig;
import com.itboye.core.object.BaseFragment;
import com.itboye.core.object.ResultEntity;
import com.itboye.core.object.SpPublicParams;
import com.itboye.core.utils.EnumUtility;
import com.itboye.core.utils.SPUtils;
import com.itboye.jmsfhz.R;
import com.itboye.jmsfhz.app.adapter.NewsDongTaiAdapter;
import com.itboye.jmsfhz.app.adapter.PersonnelAdapter;
import com.itboye.jmsfhz.app.ui.web.WebActivity;
import com.itboye.jmsfhz.data.model.HomePageModel;
import com.itboye.jmsfhz.data.model.LunBoModel;
import com.itboye.jmsfhz.data.presenter.LunBoPresenter;
import com.itboye.jmsfhz.data.presenter.Personnelpresenter;
import com.itboye.jmsfhz.widget.CarouselView;
import com.itboye.jmsfhz.widget.XListView;
import com.itboye.jmsfhz.widget.XListView.IXListViewListener;
import com.itboye.jmsfhz.widget.XScrollView;
import com.lfh.commentdemo.bean.AppModelNewsBean;
import com.lfh.commentdemo.view.MyListView;

public class EnterPriseMessgeFragment extends BaseFragment implements Observer,
		IXListViewListener {
	private List<ImageView> bmps;
	CarouselView topLunbo;
	LunBoPresenter lunBoPresenter;
	XListView mylistviewpernnel;
	TextView tvNull;
	private LunBoModel Beannermodel;
	View view;
	NewsDongTaiAdapter adapter;
	Personnelpresenter personnelpresenter;
	AppModelNewsBean bean;
	int page_num = 1;
	int page_size = 10;
	int position;

	@Override
	public int initView() {
		// TODO Auto-generated method stub
		return R.layout.enterprise_fragemnt;
	}

	@Override
	public void onMyActivityCreated() {

		Beannermodel = new LunBoModel();
		lunBoPresenter = new LunBoPresenter(this);
		Beannermodel.setPage_num(page_num + "");
		Beannermodel.setPage_size(page_size + "");
		Beannermodel.setPosition(6045);
		lunBoPresenter.getInfo(Beannermodel);

		// TODO Auto-generated method stub
		view = LayoutInflater.from(getActivity()).inflate(R.layout.item_head,
				null);

		mylistviewpernnel.addHeaderView(view);
		topLunbo = (CarouselView) getView().findViewById(R.id.topLunbo);

		bean = new AppModelNewsBean();
		personnelpresenter = new Personnelpresenter(this);
		bean.setPage_num(page_num + "");
		bean.setPage_size(page_size + "");
		bean.setCate(EnumUtility.ENTERPRISEINFORMATION.getValue() + "");
		personnelpresenter.getInfo(bean);

		// ////////
		mylistviewpernnel.setXListViewListener(this);
		/** 刷新后获取数据 **/
		mylistviewpernnel.setPullLoadEnable(true);
		/** 刷新 **/
		mylistviewpernnel.setPullRefreshEnable(true);
		mylistviewpernnel.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getActivity(), WebActivity.class);

				intent.putExtra("title", "企业信息");
				intent.putExtra("url",
						GlobalConfig.WEBURL + "Webview/Post/viewPost?id="
								+ bean.getList().get(arg2-2).getId());
				intent.putExtra("cate", bean.getCate());
				intent.putExtra("id", bean.getList().get(arg2-2).getId());
				bean.setCate(EnumUtility.ENTERPRISEINFORMATION.getValue() + "");
				startActivity(intent);
			}
		});

	}

	@Override
	public void update(Observable observable, Object data) {
		ResultEntity model = super.handlerError(data);

		if (model.getEventType() == personnelpresenter.HeZuoSuccessEvent) {
			bean = (AppModelNewsBean) model.getData();
			adapter = new NewsDongTaiAdapter(getActivity(), bean);
			mylistviewpernnel.setAdapter(adapter);
			System.out.println(">>>>>>>>>>>>>>>>>>" + model.getData());

		}
		if (model.getEventType() == lunBoPresenter.BannerSuccessEvent) {
			Beannermodel = (LunBoModel) model.getData();
			bmps = new ArrayList<ImageView>();
			for (int i = 0; i < Beannermodel.getResult().size(); i++) {
				position = i;
				ImageView imageView = new ImageView(getActivity());

				imageView.setScaleType(ScaleType.FIT_XY);
				com.nostra13.universalimageloader.core.ImageLoader
						.getInstance().displayImage(
								GlobalConfig.JIUYE
										+ Beannermodel.getResult().get(i)
												.getImg(), imageView);
//				imageView.setOnClickListener(new OnClickListener() {
//
//					@Override
//					public void onClick(View arg0) {
//						// TODO Auto-generated method stub
//
//						if (Beannermodel.getResult().get(0).getUrl() == null
//								|| Beannermodel.getResult().get(0).getUrl() == "") {
//						} else {
//							Intent intent = new Intent(getActivity(),
//									WebActivity.class);
//							intent.putExtra("url", GlobalConfig.WEBURL
//									+ Beannermodel.getResult().get(0).getUrl());
//							startActivity(intent);
//						}
//
//					}
//				});
				bmps.add(imageView);

			}

			topLunbo.setImageBitmaps(bmps);
			try {
				/**
				 * 当数据加载出来后dialog则消失
				 */
				closeProgressDialog();
			} catch (Exception e) {
			}

			stopLoad();
		}
	}

	@Override
	public void onRefresh() {
		// TODO Auto-generated method stub
		page_num = 1;
		page_size = 10;
		bean.setCate("6102");
		personnelpresenter.getInfo(bean);
	}

	@Override
	public void onLoadMore() {
		// TODO Auto-generated method stub
		page_num++;
		page_size++;
		bean.setCate("6102");
		personnelpresenter.getInfo(bean);
	}

	public void stopLoad() {
		mylistviewpernnel.stopLoadMore();
		mylistviewpernnel.stopRefresh();
	}

}
